﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class StatementDetailDA : IStatementDetailDA
    {
        private string BILLING_CREDENCE_ID = "@BILLING_CREDENCE_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string LINE_ID = "@LINE_ID";
        private string ORDER_ID = "@ORDER_ID";
        private string ORDER_LINE_ID = "@ORDER_LINE_ID";
        private string QTY = "@QTY";
        private string RECEIVABLE = "@RECEIVABLE";
        private string RECEIVED = "@RECEIVED";
        private string REMARK = "@REMARK";
        private string SKU_ID = "@SKU_ID";
        private string SOURCE_TYPE = "@SOURCE_TYPE";
        private string SQL_CHECK_STATEMENT_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@STATEMENT_DETAIL WHERE STATEMENT_ID = @STATEMENT_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_STATEMENT_DETAIL = " DELETE FROM @_@STATEMENT_DETAIL WHERE STATEMENT_ID = @STATEMENT_ID";
        private string SQL_INSERT_STATEMENT_DETAIL = " INSERT INTO @_@STATEMENT_DETAIL ( WH_ID, STATEMENT_ID, LINE_ID, ORDER_ID, ORDER_LINE_ID, SKU_ID, QTY, TYPE, SOURCE_TYPE, RECEIVABLE, RECEIVED, REMARK, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,BILLING_CREDENCE_ID) VALUES ( @WH_ID, @STATEMENT_ID, @LINE_ID, @ORDER_ID, @ORDER_LINE_ID, @SKU_ID, @QTY, @TYPE, @SOURCE_TYPE, @RECEIVABLE, @RECEIVED, @REMARK, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() ,@BILLING_CREDENCE_ID)  ";
        private string SQL_SELECT_ALL_SATAEMENT_DETAIL_LANGUAGE = "SELECT  SD.ROWID, SD.WH_ID, SD.STATEMENT_ID, SD.LINE_ID, SD.ORDER_ID, SD.ORDER_LINE_ID, SD.SKU_ID, SD.QTY, T.[TEXT] AS TYPE, SD.SOURCE_TYPE, SD.RECEIVABLE, SD.RECEIVED, SD.REMARK, SD.CREATED_BY, SD.CREATED_DATE, SD.UPDATED_BY, SD.UPDATED_DATE,SD.BILLING_CREDENCE_ID  FROM  @_@STATEMENT_DETAIL SD  LEFT JOIN  @_@SYS_CODE_DETAIL SCD ON SCD.SYS_CODE = SD.TYPE AND SCD.SYS_CODE_TYPE = 'CHARGETYPE'  LEFT JOIN SYSADM.TRANSLATION T ON 'CHARGETYPE.'+SD.TYPE = T.ITEM_ID  AND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY ='@LANGUAGE'  ";
        private string SQL_SELECT_ALL_STATEMENT_DETAIL = " SELECT  ROWID, WH_ID, STATEMENT_ID, LINE_ID, ORDER_ID, ORDER_LINE_ID, SKU_ID, QTY, TYPE, SOURCE_TYPE, RECEIVABLE, RECEIVED, REMARK, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE,BILLING_CREDENCE_ID  FROM  @_@STATEMENT_DETAIL ";
        private string SQL_SELECT_STATEMENT_GROUP = "SELECT TYPE, SUM(RECEIVED) AS RECEIVED FROM @_@STATEMENT_DETAIL WHERE STATEMENT_ID=@STATEMENT_ID GROUP BY WH_ID,TYPE";
        private string SQL_UPDATE_STATEMENT_DETAIL = " UPDATE @_@STATEMENT_DETAIL SET WH_ID = @WH_ID, ORDER_ID = @ORDER_ID, ORDER_LINE_ID = @ORDER_LINE_ID, SKU_ID = @SKU_ID, QTY = @QTY, TYPE = @TYPE, SOURCE_TYPE = @SOURCE_TYPE, RECEIVABLE = @RECEIVABLE, RECEIVED = @RECEIVED, REMARK = @REMARK, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE STATEMENT_ID = @STATEMENT_ID AND LINE_ID = @LINE_ID  ";
        private string SQL_UPDATE_STATEMENT_DETAIL_RECEIVED = " UPDATE @_@STATEMENT_DETAIL SET RECEIVED = @RECEIVED,UPDATED_BY=@UPDATED_BY, REMARK=@REMARK WHERE STATEMENT_ID = @STATEMENT_ID AND LINE_ID = @LINE_ID  ";
        private string STATEMENT_ID = "@STATEMENT_ID";
        private string TYPE = "@TYPE";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckStatementDetailIDUnique(string statementDetailID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementDetailID), new SqlParameter("@LINE_ID", lineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_STATEMENT_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckStatementDetailIDUnique(DataBase dataBase, DbTransaction tran, string statementDetailID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementDetailID), new SqlParameter("@LINE_ID", lineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_STATEMENT_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteStatementDetail(string statementdetailID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                string strSql = this.SQL_DELETE_STATEMENT_DETAIL.Replace("@_@", whLoginID) + " AND LINE_ID = @LINE_ID";
                for (int j = 0; j < lineID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementdetailID), new SqlParameter("@LINE_ID", lineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, strSql, paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteStatementDetail(DataBase dataBase, DbTransaction tran, string statementDetailID, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementDetailID) };
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_STATEMENT_DETAIL.Replace("@_@", whLoginID), paras);
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteStatementDetail(DataBase dataBase, DbTransaction tran, string statementdetailID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            try
            {
                string strSql = this.SQL_DELETE_STATEMENT_DETAIL.Replace("@_@", whLoginID) + " AND LINE_ID = @LINE_ID";
                for (int j = 0; j < lineID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementdetailID), new SqlParameter("@LINE_ID", lineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, strSql, paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllStatementDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_STATEMENT_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllStatementDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_STATEMENT_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public StatementDetailInfo GetStatementDetailByID(string statementdetailID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_STATEMENT_DETAIL.Replace("@_@", whLoginID) + " WHERE STATEMENT_ID = @STATEMENT_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementdetailID), new SqlParameter("@LINE_ID", lineID) };
            StatementDetailInfo statementDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    statementDetailInfo = new StatementDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["STATEMENT_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["SKU_ID"].ToString(), (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["TYPE"].ToString(), reader["SOURCE_TYPE"].ToString(), (reader["RECEIVABLE"].ToString() != "") ? decimal.Parse(reader["RECEIVABLE"].ToString()) : 0M, (reader["RECEIVED"].ToString() != "") ? decimal.Parse(reader["RECEIVED"].ToString()) : 0M, reader["REMARK"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["BILLING_CREDENCE_ID"].ToString());
                }
            }
            return statementDetailInfo;
        }

        public List<StatementDetailInfo> GetStatementDetailByID(string statementdetailID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_SATAEMENT_DETAIL_LANGUAGE.Replace("@_@", whLoginID).Replace("@LANGUAGE", pubParasInfo.UserLanguage) + " WHERE STATEMENT_ID = @STATEMENT_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementdetailID) };
            List<StatementDetailInfo> statementDetailInfoList = new List<StatementDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    StatementDetailInfo statementDetailInfo;
                    DateTime CS00002;
                    statementDetailInfo = statementDetailInfo = new StatementDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["STATEMENT_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["SKU_ID"].ToString(), (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["TYPE"].ToString(), reader["SOURCE_TYPE"].ToString(), (reader["RECEIVABLE"].ToString() != "") ? decimal.Parse(reader["RECEIVABLE"].ToString()) : 0M, (reader["RECEIVED"].ToString() != "") ? decimal.Parse(reader["RECEIVED"].ToString()) : 0M, reader["REMARK"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["BILLING_CREDENCE_ID"].ToString());
                    statementDetailInfoList.Add(statementDetailInfo);
                }
            }
            return statementDetailInfoList;
        }

        public StatementDetailInfo GetStatementDetailByID(DataBase dataBase, DbTransaction tran, string statementdetailID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_STATEMENT_DETAIL.Replace("@_@", whLoginID) + " WHERE STATEMENT_ID = @STATEMENT_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementdetailID), new SqlParameter("@LINE_ID", lineID) };
            StatementDetailInfo statementDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                statementDetailInfo = new StatementDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["STATEMENT_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["SKU_ID"].ToString(), (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["TYPE"].ToString(), reader["SOURCE_TYPE"].ToString(), (reader["RECEIVABLE"].ToString() != "") ? decimal.Parse(reader["RECEIVABLE"].ToString()) : 0M, (reader["RECEIVED"].ToString() != "") ? decimal.Parse(reader["RECEIVED"].ToString()) : 0M, reader["REMARK"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime(), reader["BILLING_CREDENCE_ID"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return statementDetailInfo;
        }

        public List<StatementDetailInfo> GetStatementDetailByID(DataBase dataBase, DbTransaction tran, string statementdetailID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_SATAEMENT_DETAIL_LANGUAGE.Replace("@_@", whLoginID).Replace("@LANGUAGE", pubParasInfo.UserLanguage) + " WHERE STATEMENT_ID = @STATEMENT_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementdetailID) };
            List<StatementDetailInfo> statementDetailInfoList = new List<StatementDetailInfo>();
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                StatementDetailInfo statementDetailInfo;
                DateTime CS00002;
                statementDetailInfo = statementDetailInfo = new StatementDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["STATEMENT_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["ORDER_ID"].ToString(), (reader["ORDER_LINE_ID"].ToString() != "") ? int.Parse(reader["ORDER_LINE_ID"].ToString()) : 0, reader["SKU_ID"].ToString(), (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, reader["TYPE"].ToString(), reader["SOURCE_TYPE"].ToString(), (reader["RECEIVABLE"].ToString() != "") ? decimal.Parse(reader["RECEIVABLE"].ToString()) : 0M, (reader["RECEIVED"].ToString() != "") ? decimal.Parse(reader["RECEIVED"].ToString()) : 0M, reader["REMARK"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["BILLING_CREDENCE_ID"].ToString());
                statementDetailInfoList.Add(statementDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return statementDetailInfoList;
        }

        public DataSet GetStatementDetailByQueryList(List<string> sqlWhere, StatementDetailQueryEntity statementdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_STATEMENT_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!statementdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, statementdetailQuery.CurrentPage, statementdetailQuery.PageSize, statementdetailQuery.SortField, statementdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetStatementDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, StatementDetailQueryEntity statementdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_STATEMENT_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, statementdetailQuery.CurrentPage, statementdetailQuery.PageSize, statementdetailQuery.SortField, statementdetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<StatementDetailInfo> GetStatementDetailGroupByID(string statementdetailID, string whLoginID)
        {
            string sql = this.SQL_SELECT_STATEMENT_GROUP.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementdetailID) };
            List<StatementDetailInfo> statementDetailInfoList = new List<StatementDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    StatementDetailInfo statementDetailInfo;
                    DateTime CS00002 = new DateTime();
                    CS00002 = new DateTime();
                    statementDetailInfo = statementDetailInfo = new StatementDetailInfo(0, "", "", 0, "", 0, "", 0M, reader["TYPE"].ToString(), "", 0M, (reader["RECEIVED"].ToString() != "") ? decimal.Parse(reader["RECEIVED"].ToString()) : 0M, "", "", CS00002, "", CS00002, reader["BILLING_CREDENCE_ID"].ToString());
                    statementDetailInfoList.Add(statementDetailInfo);
                }
            }
            return statementDetailInfoList;
        }

        public int InsertStatementDetail(StatementDetailInfo statementDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                statementDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_StatementDetail_Parameters(statementDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_STATEMENT_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertStatementDetail(DataBase dataBase, DbTransaction tran, StatementDetailInfo statementDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                statementDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_StatementDetail_Parameters(statementDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_STATEMENT_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_StatementDetail_Parameters(StatementDetailInfo statementDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.STATEMENT_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.ORDER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.ORDER_LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.SKU_ID, SqlDbType.VarChar, 20), new SqlParameter(this.QTY, SqlDbType.Decimal, 13), new SqlParameter(this.TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.SOURCE_TYPE, SqlDbType.VarChar, 20), new SqlParameter(this.RECEIVABLE, SqlDbType.Decimal, 13), new SqlParameter(this.RECEIVED, SqlDbType.Decimal, 13), new SqlParameter(this.REMARK, SqlDbType.VarChar, 100), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.BILLING_CREDENCE_ID, SqlDbType.VarChar, 30) };
            if (!string.IsNullOrEmpty(statementDetailInfo.WhID))
            {
                paramArray[0].Value = statementDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = statementDetailInfo.StatementID;
            paramArray[2].Value = statementDetailInfo.LineID;
            paramArray[3].Value = statementDetailInfo.OrderID;
            paramArray[4].Value = statementDetailInfo.OrderLineID;
            if (!string.IsNullOrEmpty(statementDetailInfo.SkuID))
            {
                paramArray[5].Value = statementDetailInfo.SkuID;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(statementDetailInfo.Qty.ToString()))
            {
                paramArray[6].Value = statementDetailInfo.Qty;
            }
            else
            {
                paramArray[6].Value = 0;
            }
            if (!string.IsNullOrEmpty(statementDetailInfo.Type))
            {
                paramArray[7].Value = statementDetailInfo.Type;
            }
            else
            {
                paramArray[7].Value = 0;
            }
            if (!string.IsNullOrEmpty(statementDetailInfo.SourceType))
            {
                paramArray[8].Value = statementDetailInfo.SourceType;
            }
            else
            {
                paramArray[8].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(statementDetailInfo.Receivable.ToString()))
            {
                paramArray[9].Value = statementDetailInfo.Receivable;
            }
            else
            {
                paramArray[9].Value = 0;
            }
            if (!string.IsNullOrEmpty(statementDetailInfo.Received.ToString()))
            {
                paramArray[10].Value = statementDetailInfo.Received;
            }
            else
            {
                paramArray[10].Value = 0;
            }
            if (!string.IsNullOrEmpty(statementDetailInfo.Remark))
            {
                paramArray[11].Value = statementDetailInfo.Remark;
            }
            else
            {
                paramArray[11].Value = DBNull.Value;
            }
            paramArray[12].Value = statementDetailInfo.CreatedBy;
            paramArray[13].Value = statementDetailInfo.UpdatedBy;
            if (!string.IsNullOrEmpty(statementDetailInfo.BillingCredenceID))
            {
                paramArray[14].Value = statementDetailInfo.BillingCredenceID;
                return paramArray;
            }
            paramArray[14].Value = DBNull.Value;
            return paramArray;
        }

        public int UpdateStatementDetail(StatementDetailInfo statementDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                statementDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_StatementDetail_Parameters(statementDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_STATEMENT_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateStatementDetail(DataBase dataBase, DbTransaction tran, StatementDetailInfo statementDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                statementDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_StatementDetail_Parameters(statementDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_STATEMENT_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateStatementDetailReceived(StatementDetailInfo statementDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                statementDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@STATEMENT_ID", statementDetailInfo.StatementID), new SqlParameter("@LINE_ID", statementDetailInfo.LineID), new SqlParameter("@RECEIVED", statementDetailInfo.Received), new SqlParameter("@UPDATED_BY", statementDetailInfo.UpdatedBy), new SqlParameter("@REMARK", statementDetailInfo.Remark) };
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_STATEMENT_DETAIL_RECEIVED.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }
    }
}

